package com.yaozheng.vocationaltraining.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.CateWinRateBean;
import com.yaozheng.vocationaltraining.bean.CompareUserInfoBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Created by moon on 2017/11/19.
 */

public class CompareDetailDialog {
    private BitmapLoader userBitmapLoader;

    private Context context;
    private Dialog dialog;
    private LinearLayout lLayout_bg;
    private ImageView closed_imageview;

    private CircleImageView my_userAvatarImage;
    private CircleImageView other_userAvatarImage;
    private TextView my_name_textview;
    private TextView other_name_textview;
    private ProgressTextView my_score_progressTextView;
    private ProgressTextView other_score_progressTextView;
    private TextView my_store_name_textview;
    private TextView other_store_name_textview;
    private TextView my_position_textview;
    private TextView other_position_textview;
    private ProgressTextView my_win_rate_progressTextView;
    private ProgressTextView other_win_rate_progressTextView;
    private ProgressTextView my_pk_times_progressTextView;
    private ProgressTextView other_pk_times_progressTextView;
    private ProgressTextView my_brand_win_rate_progressTextView;
    private ProgressTextView other_brand_win_rate_progressTextView;
    private ProgressTextView my_bx7_win_rate_progressTextView;
    private ProgressTextView other_bx7_win_rate_progressTextView;
    private ProgressTextView my_bx5_win_rate_progressTextView;
    private ProgressTextView other_bx5_win_rate_progressTextView;
    private ProgressTextView my_sale_win_rate_progressTextView;
    private ProgressTextView other_sale_win_rate_progressTextView;
    private ProgressTextView my_server_win_rate_progressTextView;
    private ProgressTextView other_server_win_rate_progressTextView;
    private ProgressTextView my_technique_win_rate_progressTextView;
    private ProgressTextView other_technique_win_rate_progressTextView;
    private ProgressTextView my_job_win_rate_progressTextView;
    private ProgressTextView other_job_win_rate_progressTextView;
    private ProgressTextView my_brand_times_progressTextView;
    private ProgressTextView other_brand_times_progressTextView;
    private ProgressTextView my_bx7_times_progressTextView;
    private ProgressTextView other_bx7_times_progressTextView;
    private ProgressTextView my_bx5_times_progressTextView;
    private ProgressTextView other_bx5_times_progressTextView;

    private ProgressTextView my_sale_times_progressTextView;
    private ProgressTextView other_sale_times_progressTextView;
    private ProgressTextView my_server_times_progressTextView;
    private ProgressTextView other_server_times_progressTextView;
    private ProgressTextView my_technique_times_progressTextView;
    private ProgressTextView other_technique_times_progressTextView;
    private ProgressTextView my_job_times_progressTextView;
    private ProgressTextView other_job_times_progressTextView;





    private Display display;

    private ArrayList<CompareUserInfoBean>  compareUserInfoBeanArrayList;

    DecimalFormat df = new DecimalFormat("#.00");


    public CompareDetailDialog(Context context,ArrayList<CompareUserInfoBean> compareUserInfoBeanArrayList) {
        this.context = context;
        this.compareUserInfoBeanArrayList = compareUserInfoBeanArrayList;
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public CompareDetailDialog builder() {
        //
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_compare_detail_dailog, null);

        initViewUI(view);

        initViewData();

        //
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);

        //
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.9), LinearLayout.LayoutParams.WRAP_CONTENT));

        closed_imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        return this;
    }

    private void initViewData() {
        if(compareUserInfoBeanArrayList != null && compareUserInfoBeanArrayList.size() ==2 ){
            CompareUserInfoBean myInfo = compareUserInfoBeanArrayList.get(0);
            CompareUserInfoBean otherInfo = compareUserInfoBeanArrayList.get(1);

            //加载图片
            userBitmapLoader.displayImage(myInfo.getAvatar(), my_userAvatarImage);
            userBitmapLoader.displayImage(otherInfo.getAvatar(), other_userAvatarImage);

            my_name_textview.setText(myInfo.getName());
            other_name_textview.setText(otherInfo.getName());
            my_score_progressTextView.setText(myInfo.getScore()+"");
            my_score_progressTextView.setRate((int)(myInfo.getScore().floatValue()/(myInfo.getScore() + otherInfo.getScore()) * 10000));
            other_score_progressTextView.setText(otherInfo.getScore()+"");
            other_score_progressTextView.setRate((int)(otherInfo.getScore().floatValue()/(myInfo.getScore() + otherInfo.getScore()) * 10000));

            my_store_name_textview.setText(myInfo.getStoreName());
            other_store_name_textview.setText(otherInfo.getStoreName());
            my_position_textview.setText((myInfo.getPosition()==null||"null".equals(myInfo.getPosition()))?"":myInfo.getPosition());
            other_position_textview.setText((otherInfo.getPosition()==null || "null".equals(otherInfo.getPosition()))?"":otherInfo.getPosition());

            my_win_rate_progressTextView.setText(df.format(myInfo.getWinRate().floatValue() / 100)+"");
            my_win_rate_progressTextView.setRate(myInfo.getWinRate());
            other_win_rate_progressTextView.setText(df.format(otherInfo.getWinRate().floatValue() / 100)+"");
            other_win_rate_progressTextView.setRate(otherInfo.getWinRate());
            my_pk_times_progressTextView.setText(myInfo.getPkTimes()+"");
            my_pk_times_progressTextView.setRate((int)(myInfo.getPkTimes().floatValue()/(myInfo.getPkTimes()+otherInfo.getPkTimes()) * 10000));
            other_pk_times_progressTextView.setText(otherInfo.getPkTimes()+"");
            other_pk_times_progressTextView.setRate((int)(otherInfo.getPkTimes().floatValue()/(myInfo.getPkTimes()+otherInfo.getPkTimes()) * 10000));

            float my_brand_times = 0;
            float my_bx7_times = 0;
            float my_bx5_times = 0;
            float my_sale_times = 0;
            float my_server_times = 0;

            float other_brand_times = 0;
            float other_bx7_times = 0;
            float other_bx5_times = 0;
            float other_sale_times = 0;
            float other_server_times = 0;

            for(CateWinRateBean bean : myInfo.getCateWinRates()){
                if (bean.getName().contains("品牌理解")){//品牌理解
                    my_brand_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("BX7")){
                    my_bx7_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("BX5")){
                    my_bx5_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("销售基础")){
                    my_sale_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("服务基础")){
                    my_server_times = bean.getTimes() <= 0 ?0:bean.getTimes();;
                }
            }

            for(CateWinRateBean bean : otherInfo.getCateWinRates()){
                if (bean.getName().contains("品牌理解")){//品牌理解
                    other_brand_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("BX7")){
                    other_bx7_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("BX5")){
                    other_bx5_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("销售基础")){
                    other_sale_times = bean.getTimes() <= 0 ?0:bean.getTimes();
                }else if (bean.getName().contains("服务基础")){
                    other_server_times = bean.getTimes() <= 0 ?0:bean.getTimes();;
                }
            }


            for(CateWinRateBean bean : myInfo.getCateWinRates()){
                if (bean.getName().contains("品牌理解")){//品牌理解
                    my_brand_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    my_brand_win_rate_progressTextView.setRate(bean.getRate());

                    my_brand_times_progressTextView.setText(bean.getTimes()+"");
                    my_brand_times_progressTextView.setRate(my_brand_times==0?0:
                            (int)(my_brand_times/(my_brand_times + other_brand_times) * 10000));
                }else if (bean.getName().contains("BX7")){
                    my_bx7_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    my_bx7_win_rate_progressTextView.setRate(bean.getRate());

                    my_bx7_times_progressTextView.setText(bean.getTimes()+"");
                    my_bx7_times_progressTextView.setRate(my_bx7_times==0?0:
                            (int)(my_bx7_times/(my_bx7_times + other_bx7_times) * 10000));
                }else if (bean.getName().contains("BX5")){
                    my_bx5_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    my_bx5_win_rate_progressTextView.setRate(bean.getRate());

                    my_bx5_times_progressTextView.setText(bean.getTimes()+"");
                    my_bx5_times_progressTextView.setRate(my_bx5_times==0?0:
                            (int)(my_bx5_times/(my_bx5_times + other_bx5_times) * 10000));
                }else if (bean.getName().contains("销售基础")){
                    my_sale_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    my_sale_win_rate_progressTextView.setRate(bean.getRate());

                    my_sale_times_progressTextView.setText(bean.getTimes()+"");
                    my_sale_times_progressTextView.setRate(my_sale_times==0?0:
                            (int)(my_sale_times/(my_sale_times + other_sale_times) * 10000));
                }else if (bean.getName().contains("服务基础")){
                    my_server_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    my_server_win_rate_progressTextView.setRate(bean.getRate());

                    my_server_times_progressTextView.setText(bean.getTimes()+"");
                    my_server_times_progressTextView.setRate(my_server_times==0?0:
                            (int)(my_server_times/(my_server_times + other_server_times) * 10000));
                }
            }

            for(CateWinRateBean bean : otherInfo.getCateWinRates()){
                if (bean.getName().contains("品牌理解")){//品牌理解
                    other_brand_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    other_brand_win_rate_progressTextView.setRate(bean.getRate());

                    other_brand_times_progressTextView.setText(bean.getTimes()+"");
                    other_brand_times_progressTextView.setRate(other_brand_times==0?0:
                            (int)(other_brand_times/(my_brand_times + other_brand_times) * 10000));
                }else if (bean.getName().contains("BX7")){
                    other_bx7_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    other_bx7_win_rate_progressTextView.setRate(bean.getRate());

                    other_bx7_times_progressTextView.setText(bean.getTimes()+"");
                    other_bx7_times_progressTextView.setRate(other_bx7_times==0?0:
                            (int)(other_bx7_times/(my_bx7_times + other_bx7_times) * 10000));
                }else if (bean.getName().contains("BX5")){
                    other_bx5_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    other_bx5_win_rate_progressTextView.setRate(bean.getRate());

                    other_bx5_times_progressTextView.setText(bean.getTimes()+"");
                    other_bx5_times_progressTextView.setRate(other_bx5_times==0?0:
                            (int)(other_bx5_times/(my_bx5_times + other_bx5_times) * 10000));
                }else if (bean.getName().contains("销售基础")){
                    other_sale_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    other_sale_win_rate_progressTextView.setRate(bean.getRate());

                    other_sale_times_progressTextView.setText(bean.getTimes()+"");
                    other_sale_times_progressTextView.setRate(other_sale_times==0?0:
                            (int)(other_sale_times/(my_sale_times + other_sale_times) * 10000));
                }else if (bean.getName().contains("服务基础")){
                    other_server_win_rate_progressTextView.setText(bean.getRate().floatValue() / 100 +"");
                    other_server_win_rate_progressTextView.setRate(bean.getRate());

                    other_server_times_progressTextView.setText(bean.getTimes()+"");
                    other_server_times_progressTextView.setRate(other_server_times==0?0:
                            (int)(other_server_times/(my_server_times + other_server_times) * 10000));
                }
            }

//            other_brand_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_brand_win_rate_progressTextView);
//            my_bx7_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx7_win_rate_progressTextView);
//            other_bx7_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx7_win_rate_progressTextView);
//            my_bx5_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx5_win_rate_progressTextView);
//            other_bx5_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx5_win_rate_progressTextView);
//            my_sale_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_sale_win_rate_progressTextView);
//            other_sale_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_sale_win_rate_progressTextView);
//            my_server_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_server_win_rate_progressTextView);
//            other_server_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_server_win_rate_progressTextView);
//            my_technique_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_technique_win_rate_progressTextView);
//            other_technique_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_technique_win_rate_progressTextView);
//            my_job_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_job_win_rate_progressTextView);
//            other_job_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_job_win_rate_progressTextView);
//
//            my_brand_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_brand_times_progressTextView);
//            other_brand_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_brand_times_progressTextView);
//            my_bx7_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx7_times_progressTextView);
//            other_bx7_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx7_times_progressTextView);
//            my_bx5_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx5_times_progressTextView);
//            other_bx5_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx5_times_progressTextView);
//            my_sale_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_sale_times_progressTextView);
//            other_sale_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_sale_times_progressTextView);
//            my_server_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_server_times_progressTextView);
//            other_server_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_server_times_progressTextView);
//            my_technique_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_technique_times_progressTextView);
//            other_technique_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_technique_times_progressTextView);
//            my_job_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_job_times_progressTextView);
//            other_job_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_job_times_progressTextView);
        }

    }

    private void initViewUI(View view) {
        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
        closed_imageview = (ImageView) view.findViewById(R.id.closed_imageview);


        my_userAvatarImage = (CircleImageView) view.findViewById(R.id.my_userAvatarImage);
        other_userAvatarImage = (CircleImageView) view.findViewById(R.id.other_userAvatarImage);
        my_name_textview = (TextView) view.findViewById(R.id.my_name_textview);
        other_name_textview = (TextView) view.findViewById(R.id.other_name_textview);
        my_score_progressTextView = (ProgressTextView) view.findViewById(R.id.my_score_progressTextView);
        other_score_progressTextView = (ProgressTextView) view.findViewById(R.id.other_score_progressTextView);
        my_store_name_textview = (TextView) view.findViewById(R.id.my_store_name_textview);
        other_store_name_textview = (TextView) view.findViewById(R.id.other_store_name_textview);
        my_position_textview = (TextView) view.findViewById(R.id.my_position_textview);
        other_position_textview = (TextView) view.findViewById(R.id.other_position_textview);
        my_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_win_rate_progressTextView);
        other_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_win_rate_progressTextView);
        my_pk_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_pk_times_progressTextView);
        other_pk_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_pk_times_progressTextView);
        my_brand_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_brand_win_rate_progressTextView);
        other_brand_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_brand_win_rate_progressTextView);
        my_bx7_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx7_win_rate_progressTextView);
        other_bx7_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx7_win_rate_progressTextView);
        my_bx5_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx5_win_rate_progressTextView);
        other_bx5_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx5_win_rate_progressTextView);
        my_sale_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_sale_win_rate_progressTextView);
        other_sale_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_sale_win_rate_progressTextView);
        my_server_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_server_win_rate_progressTextView);
        other_server_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_server_win_rate_progressTextView);
        my_technique_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_technique_win_rate_progressTextView);
        other_technique_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_technique_win_rate_progressTextView);
        my_job_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.my_job_win_rate_progressTextView);
        other_job_win_rate_progressTextView = (ProgressTextView) view.findViewById(R.id.other_job_win_rate_progressTextView);
        my_brand_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_brand_times_progressTextView);
        other_brand_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_brand_times_progressTextView);
        my_bx7_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx7_times_progressTextView);
        other_bx7_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx7_times_progressTextView);
        my_bx5_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_bx5_times_progressTextView);
        other_bx5_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_bx5_times_progressTextView);

        my_sale_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_sale_times_progressTextView);
        other_sale_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_sale_times_progressTextView);
        my_server_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_server_times_progressTextView);
        other_server_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_server_times_progressTextView);
        my_technique_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_technique_times_progressTextView);
        other_technique_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_technique_times_progressTextView);
        my_job_times_progressTextView = (ProgressTextView) view.findViewById(R.id.my_job_times_progressTextView);
        other_job_times_progressTextView = (ProgressTextView) view.findViewById(R.id.other_job_times_progressTextView);

    }

    public void show() {
       // setLayout();
        dialog.show();
    }
}
